<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;

use app\common\logic\User as CommonUser;
use app\common\logic\ProductFollow as CommonProductFollow;
use app\api\error\CodeBase;
use app\api\error\Common;

/**
 * 用户接口逻辑
 */
class User extends ApiBase
{
    
    public static $commonUserLogic = null;
    public static $commonProductFollowLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonUserLogic) && static::$commonUserLogic = new CommonUser();
        empty(static::$commonProductFollowLogic) && static::$commonProductFollowLogic = new CommonProductFollow();
    }
    


    /**
     * 获取用户信息
     */
    public function getUserInfo($data = [])
    {
        // 验证参数
        $this->validateUser->scene('get_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getUserInfo($data);
    }

    /**
     * 更新用户信息
     */
    public function updateUserInfo($data = [])
    {
        // 验证参数
        $this->validateUser->scene('update_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->updateUserInfo($data);
    }

    /**
     * 获取用户统计信息
     */
    public function getUserStats($data = [])
    {
        // 验证参数
        $this->validateUser->scene('invite_stats')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getUserStats($data);
    }

    /**
     * 获取用户VIP状态
     */
    public function getVipStatus($data = [])
    {
        // 验证参数
        $this->validateUser->scene('get_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getVipStatus($data);
    }

    /**
     * 检查并更新用户VIP状态
     * @author 王勇程 <laughskydragon@qq.com>
     * @param array $data
     * @return array
     */
    public function checkAndUpdateVipStatus($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }

        // 调用common层业务逻辑
        return static::$commonUserLogic->checkAndUpdateVipStatus($data);
    }

    /**
     * 关注商品
     */
    public function followProduct($data = [])
    {
        // 验证参数
        $this->validateUser->scene('follow')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonProductFollowLogic->followProduct($data);
    }

    /**
     * 取消关注商品
     */
    public function unfollowProduct($data = [])
    {
        // 验证参数
        $this->validateUser->scene('unfollow')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonProductFollowLogic->unfollowProduct($data);
    }

    /**
     * 设置价格提醒
     */
    public function setPriceAlert($data = [])
    {
        // 验证参数
        $this->validateUser->scene('price_alert')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->setPriceAlert($data);
    }

    /**
     * 获取关注列表
     * @author 王勇程 <laughskydragon@qq.com>
     * @param array $data
     * @return array
     */
    public function getFollowList($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }

        // 2. 调用getWhere方法
        $where = static::$commonUserLogic->getWhere($data);

        // 3. 调用common层业务逻辑
        $result = static::$commonUserLogic->getFollowList($where);

        return $result;
    }

    /**
     * 获取邀请统计
     */
    public function getInviteStats($data = [])
    {
        // 验证参数
        $this->validateUser->scene('invite_stats')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getInviteStats($data);
    }

    /**
     * 获取邀请记录
     * @author 王勇程 <laughskydragon@qq.com>
     * @param array $data
     * @return array
     */
    public function getInviteRecords($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }

        // 2. 调用getWhere方法
        $where = static::$commonUserLogic->getWhere($data);

        // 3. 调用common层业务逻辑
        $result = static::$commonUserLogic->getInviteRecords($where);

        return $result;
    }

    /**
     * 生成邀请二维码
     */
    public function generateInviteQr($data = [])
    {
        // 验证参数
        $this->validateUser->scene('invite_stats')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->generateInviteQr($data);
    }

    /**
     * 检查商品关注状态
     */
    public function checkFollowStatus($data = [])
    {
        // 验证参数
        $this->validateUser->scene('follow')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonProductFollowLogic->checkUserFollow($data);
    }

    /**
     * 检查用户状态
     */
    public function checkUserStatus($data = [])
    {
        // 验证参数
        $this->validateUser->scene('check_status')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->checkUserStatus($data);
    }

    /**
     * 获取个人中心数据
     */
    public function getProfileData($data = [])
    {
        // 验证参数
        $this->validateUser->scene('get_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getProfileData($data);
    }
    
    /**
     * 获取微信群二维码
     */
    public function getWechatGroupQrcode($data = [])
    {
        // 验证参数
        $this->validateUser->scene('get_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getWechatGroupQrcode($data);
    }
    
    /**
     * 获取设置信息
     */
    public function getSettings($data = [])
    {
        // 验证参数
        $this->validateUser->scene('get_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getSettings($data);
    }
    
    /**
     * 更新设置信息
     */
    public function updateSettings($data = [])
    {
        // 验证参数
        $this->validateUser->scene('update_settings')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->updateSettings($data);
    }

    /**
     * 个人中心首页综合数据
     * 包含：用户信息、VIP状态、统计数据、快捷功能、最新消息
     */
    public function getDashboard($data = [])
    {
        // 验证参数
        $this->validateUser->scene('get_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getDashboard($data);
    }

    /**
     * 我的资产页面综合数据
     * 包含：余额信息、邀请收益、提现记录、收益统计
     */
    public function getMyAssets($data = [])
    {
        // 验证参数
        $this->validateUser->scene('get_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getMyAssets($data);
    }

    /**
     * 我的关注页面综合数据
     * 包含：关注列表、价格提醒、关注统计、推荐商品
     */
    public function getMyFollows($data = [])
    {
        // 验证参数
        $this->validateUser->scene('get_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getMyFollows($data);
    }

    /**
     * 个人设置页面综合数据
     * 包含：用户信息、系统设置、隐私设置、通知设置
     */
    public function getSettingsPage($data = [])
    {
        // 验证参数
        $this->validateUser->scene('get_info')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->getSettingsPage($data);
    }

    /**
     * 重置密码
     */
    public function resetPassword($data = [])
    {
        // 验证参数
        $this->validateUser->scene('reset_password')->check($data);
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->resetPassword($data);
    }



    /**
     * 绑定手机号
     */
    public function bindMobile($data = [])
    {
        // 验证参数
        $this->validateUser->scene('bind_mobile')->check($data);
        
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用common层业务逻辑
        return static::$commonUserLogic->bindMobile($data);
    }
}